var user = document.getElementsByTagName("input");
var ccc = document.getElementsByClassName("ccc");

window.onload = function(){
//账号
    user[0].onblur = function(){
        if(!user[0].value){
            ccc[0].innerText = "不能为空";
            ccc[0].style.color = "red";
        }else{
            doAjax("/user/jiaoyan?username="+user[0].value,"jy")
        }
    }
    //密码
    user[1].onblur = function(){
        var z = /^\w{6,}$/;
        if(z.test(user[1].value)||user[1].value==""){
            ccc[1].innerText = "OK";
            ccc[1].style.color = "blue";
        }else{
            ccc[1].innerText = "不能少于6位";
            ccc[1].style.color = "red";
        }
        var tianjia = document.getElementsByClassName("queren")[0];
        if(tianjia.innerText == "确认添加"){
            if(user[1].value==""){
                ccc[1].innerText = "不能为空";
                ccc[1].style.color = "red";
            }
        }
    }
//    业主姓名
    user[2].onblur = function(){
        if(!user[2].value){
            ccc[2].innerText = "不能为空";
            ccc[2].style.color = "red";
        }else{
            ccc[2].innerText = "OK";
            ccc[2].style.color = "blue";
        }
    }
    //手机号
   user[3].onblur = function(){
           var z = /^((13[0-9])|(14[5-9])|(15([0-3]|[5-9]))|(16[6-7])|(17[1-8])|(18[0-9])|(19[1|3])|(19[5|6])|(19[8|9]))\d{8}$/;
           if(!user[3].value){
               ccc[3].innerText = "不能为空";
               ccc[3].style.color = "red";
           }else if(!z.test(user[3].value)){
               ccc[3].innerText = "手机号格式错误";
               ccc[3].style.color = "red";
           }else{
               ccc[3].innerText = "OK";
               ccc[3].style.color = "blue";
           }
       }
   //具体位置
   user[4].onblur = function(){
        if(!user[4].value){
            ccc[4].innerText = "不能为空";
            ccc[4].style.color = "red";
        }else{
            ccc[4].innerText = "OK";
            ccc[4].style.color = "blue";
        }
    }
}

//添加业主,修改业主
function userAddXiuGai(cz){
    var xiaoqu = document.getElementsByTagName("select")[0];
    var index = xiaoqu.selectedIndex;
    for(var i=0;i<user.length;i++){
        user[i].focus();
        user[i].blur();
    }
    if(ccc[0].innerText=="OK"&&ccc[1].innerText=="OK"&&ccc[2].innerText=="OK"&&ccc[3].innerText=="OK"&&ccc[4].innerText=="OK"&&xiaoqu.value!=""){
        doAjax("/user/"+cz+"?username="+user[0].value+"&password="+user[1].value
                +"&realname="+user[2].value+"&phone="+user[3].value+"&jtwz="+user[4].value+"&x_id="+xiaoqu.options[index].value);
    }else{
        alert("请在次确认消息是否有误");
    }
}

var xmlhttp;
function createXMLHttpRequest(){
	xmlhttp = new XMLHttpRequest();
	if(xmlhttp.overrideMimeType){
		xmlhttp.overrideMimeType("text/xml");
	}
}
function doAjax(url,s){
	createXMLHttpRequest();
	if(xmlhttp != null){
	    if(s=="jy"){
		    xmlhttp.onreadystatechange = processRequestjy;
	    }else{
		    xmlhttp.onreadystatechange = processRequest;
	    }
		xmlhttp.open("post",url,true,"","");
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send(null);
	}else {
		alert("XMLHttpRequest对象未初始化!");
	}
}

function processRequestjy(){
	if(xmlhttp.readyState == 4){
		if(xmlhttp.status == 200){
			var jieguo = xmlhttp.responseText
			if(jieguo==null||jieguo==""){
			    ccc[0].innerText = "OK";
                ccc[0].style.color = "blue";
			}else{
                ccc[0].innerText = "已存在";
                ccc[0].style.color = "red";
			}
		}else{
			alert("校严用户名Ajax请求未正常放回!\nError:"+xmlhttp.status+""+xmlhttp.statusText);
		}
	}
}

function processRequest(){
	if(xmlhttp.readyState == 4){
		if(xmlhttp.status == 200){
			var jieguo = xmlhttp.responseText;
            alert(jieguo);
		}else{
			alert("校严用户名Ajax请求未正常放回!\nError:"+xmlhttp.status+""+xmlhttp.statusText);
		}
	}
}